import 'package:dio/dio.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_user.dart';
import '../../../../common/user_helper.dart';
import '../../../../net/dio_utils.dart';
import '../../../../utils/log_util.dart';

class HomeMainV2PageController extends GetxController {
  int noReadBadgetCount = 0;

  ///获取未读消息数量
  Future<bool> requeshNoReadMessageCunnt() async {
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> map2 = {};
    map2["mineId"] = userBean.mineId;
    int userId = userBean.userId;
    map2["userId"] = userId;
    ResponseInfo responseInfo = await DioUtils.instance
        .postRequest(url: HttpHelper.trainNoReadMessage, jsonMap: map2);
    if (responseInfo.success) {
      //解析数据
      noReadBadgetCount = responseInfo.data;
      LogUtils.e("获取未读消息数量成功 $noReadBadgetCount");
    } else {
      noReadBadgetCount = 0;
    }
    update(["homeBadget"]);
    return true;
  }

  CancelToken _signCancelToken;

  ///已读消息
  void readMessage(int trainId) async {
    if (trainId == null) {
      return;
    }
    if (_signCancelToken != null && !_signCancelToken.isCancelled) {
      _signCancelToken.cancel();
    }
    _signCancelToken = new CancelToken();
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.trainMessageReadFromTrainId,
        cancelTag: _signCancelToken,
        jsonMap: {
          "trainId": trainId,
        });
    requeshNoReadMessageCunnt();
  }

  //更新视频播放的进度
  CancelToken _courseToken;
  Future<bool> homeUpdateVideo(
      {int videoId,
      int current,
      int total,
      int videoCourseId,
      bool finish = false,
      int trainId}) async {
    if (_courseToken != null && !_courseToken.isCancelled) {
      _courseToken.cancel();
    }
    _courseToken = new CancelToken();
    Map<String, dynamic> jsonMap = {
      "videoId": videoId,
      "trainId": trainId,
      "finish": finish,
      "studySecond": current,
      "videoSecond": total,
      "videoCourseId": videoCourseId,
    };
    LogUtils.e("视频进度更新 studySecond $current videoSecond $total");
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.videoStudyProgressUpdateUrl,
        cancelTag: _courseToken,
        encrypt: true,
        jsonMap: jsonMap);

    if (statisRresponseInfo.success) {
      return true;
    } else {}

    return false;
  }
}
